﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using YiSha.Web.Code;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-12-29 18:01
    /// 描 述：Courses业务类
    /// </summary>
    public class CoursesBLL
    {
        private CoursesService coursesService = new CoursesService();
        private CustomerUserService customerUserService = new CustomerUserService();

        #region 获取数据
        public async Task<TData<List<CoursesEntity>>> GetList(CoursesListParam param)
        {
            TData<List<CoursesEntity>> obj = new TData<List<CoursesEntity>>();
            obj.Data = await coursesService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<CoursesEntity>>> GetPageList(CoursesListParam param, Pagination pagination)
        {
            TData<List<CoursesEntity>> obj = new TData<List<CoursesEntity>>();
            obj.Data = await coursesService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<CoursesDto>> GetEntity(long id)
        {
            TData<CoursesDto> obj = new TData<CoursesDto>();
            obj.Data = await coursesService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(CoursesDto entity)
        {
            TData<string> obj = new TData<string>();

            OperatorInfo operatorInfo = await Operator.Instance.Current();

            if(string.IsNullOrEmpty(operatorInfo.openid) && entity.Id <= 0)
            {
                obj.Message = "请绑定微信";
                obj.Tag = 0;
                return obj;
            }

            CustomerUserListParam param = new CustomerUserListParam();
            param.openid = operatorInfo.openid;

            var cuser = await customerUserService.GetList(param);
             
            entity.user_id = cuser.FirstOrDefault().Id;

            await coursesService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await coursesService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
